//
//  YXYComposeToolBar.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/7/3.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit

enum ComposeToolBarType: Int {
    case Picture = 0
    case Mention = 1
    case Trend = 2
    case Emoticon = 3
    case Add = 4
}

class YXYComposeToolBar: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var closure:((type: ComposeToolBarType)->())?
    
    // MARK: - 添加子控件
    private func setUpUI () {
        backgroundColor = UIColor.whiteColor()
        let pictureBtn = addChildBtn("compose_toolbar_picture",type: .Picture)
        let mentionBtn = addChildBtn("compose_mentionbutton_background", type: .Mention)
        let trendBtn = addChildBtn("compose_trendbutton_background", type: .Trend)
        let emoticonBtn = addChildBtn("compose_emoticonbutton_background", type: .Emoticon)
        let addBtn = addChildBtn("compose_add_background", type: .Add)
        
        addSubview(pictureBtn)
        addSubview(mentionBtn)
        addSubview(trendBtn)
        addSubview(emoticonBtn)
        addSubview(addBtn)
        
        // 添加约束
        pictureBtn.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionBtn)
        }
        mentionBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureBtn.snp_trailing)
            make.width.equalTo(trendBtn)
        }
        trendBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionBtn.snp_trailing)
            make.width.equalTo(emoticonBtn)
        }
        emoticonBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendBtn.snp_trailing)
            make.width.equalTo(addBtn)
        }
        addBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(emoticonBtn.snp_trailing)
            make.trailing.equalTo(self)
        }
    }
    
    // MARK: - 创建子控件
    private func addChildBtn (imgName: String, type: ComposeToolBarType) -> UIButton {
        let button = UIButton()
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "clickButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置tag值  rawValue 将枚举值转成 Int
        button.tag = type.rawValue
        
        return button
    }
    
    @objc private func clickButton (button: UIButton) {
        
        closure?(type: ComposeToolBarType(rawValue: button.tag)!)
    }
    
}
